﻿using DataTurnClient.ViewModel;
using ReactiveUI;
using Splat;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataTurnClient.Controls
{
    public partial class TimerView : UserControl, IViewFor<TimerViewModel>
    {
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (TimerViewModel)value;
        }

        public TimerViewModel ViewModel { get; set; }

        public TimerView()
        {
            InitializeComponent();
            InitBindings();
            select1.SelectedIndexChanged += Select1_SelectedIndexChanged;
        }

        private void Select1_SelectedIndexChanged(object sender, AntdUI.IntEventArgs e)
        {
            ViewModel.Indexs = select1.SelectedIndex;
        }

        public void InitBindings()
        {
            ViewModel = new TimerViewModel();
            this.WhenActivated(d =>
            {
                this.badge1.Events().Click.Do(x =>
                {
                    this.Dispose();
                }).Subscribe().DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Job.JobRunTime,
                    v => v.input1.Text)
                    .DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.ErrorMessage,
                    v => v.label9.Text)
                    .DisposeWith(d);

                button1.Events().Click.Do(x =>
                {
                    ViewModel.SaveJobCommand.Execute().Subscribe(result =>
                    {
                        if (!result.Success)
                        {
                            ViewModel.ErrorMessage = result.Message;
                        }
                    });
                }).Subscribe().DisposeWith(d);

                this.Events().Load.Do(x =>
                {
                    ViewModel.SelectJobCommand.Execute().Subscribe(result =>
                    {
                        if (result.Success)
                        {
                            AntdUI.BaseCollection baseDatas = new AntdUI.BaseCollection();
                            baseDatas.AddRange(result.Data.Select(y => y.JobCode + "-" + y.JobName).ToArray());
                            select1.Items = baseDatas;
                            select1.SelectedIndex = 0;
                        }
                    });
                }).Subscribe();

            });
        }
    }
}
